﻿#include <fstream>
#include <sstream>
#include <locale>
#include "./ConfigFile.h"
#include "EagleDefine.h"
#include "EagleApi.h"
#include "QMessageBox"
#include "EagleMainFrame.h"
#include <QTextStream>

extern EagleMainFrame* g_theApp;

using namespace std;

static const QString CONFIG_FILE_NAME = QString("configure");
static const QString PRIFIX = QString("_CCD");
#define DEBUG_CONFIG_FILE 0


MySetting::MySetting(QString strNam)
{
	//Qt 无法解析 带逗号的字符串 [11/9/2016 ejitec]
	QFile file(strNam);
	if (!file.exists())
	{
		qDebug() << "configFile:" << strNam << ",not exist!!";
	}
	
	_ptSet = new QSettings(strNam, QSettings::IniFormat);
	_ptSet->setIniCodec("utf-8");

}


void EagleConfigFile::display_config()
{
	QString msg = "task_name " + task_name;msg += "\r\nconmunicate_method ";
	msg += conmunicate_method; msg += "\r\nstore_method ";
	msg += store_method; msg += "\r\ntrigger_source ";
	msg += trigger_source; msg += "\r\ndata_source ";
	msg += data_source; msg += "\r\ntrigger_type ";
	msg += trigger_type; msg += "\r\ndisk_threshold ";
	msg += disk_threshold; msg += "\r\nrecord_time ";
	msg += record_time; msg += "\r\nexport_log ";
	msg += export_log; msg += "\r\nexport_head ";
	msg += export_head; msg += "\r\npassword ";
	msg += password; msg += "\r\nserial_port ";
	msg += serial_port; msg += "\r\nlocal_task_scan_depth ";
	msg += local_task_scan_depth; msg += "\r\n";
	g_theApp->doWorkInUi_2([=]()
	{
		QMessageBox::about(g_theApp, "config file", msg);
	});
}

bool EagleConfigFile::parse_ccd(MySetting &conf)
{
	for (int i = 1; i <= 4; i++){
		CcdConfig config;
		config.serial = QString::number(i);//ccd serial start from 1 and cannot be same, and must be continuous
		if (!conf.Read(PRIFIX + config.serial + ("_Name"), &config.name))
			break;
		conf.Read(PRIFIX + config.serial + ("_PixLine"), &config.pix_line);
		conf.Read(PRIFIX + config.serial + ("_PixColumn"), &config.pix_col);
		conf.Read(PRIFIX + config.serial + ("_ColorDepth"), &config.color_depth);
		conf.Read(PRIFIX + config.serial + ("_Taps"), &config.taps);
		ccd_config[config.serial] = config;
	}
	return true;
}


bool EagleConfigFile::load()
{
	MySetting conf(CONFIG_FILE_NAME);
	if (conf.IsOk() == false)
	{
		return false;
	}

	if (!parse_ccd(conf))
	{
		return false;
	}

	conf.Read(("_TaskName"), &task_name);
	conf.Read(("_ConmunicateMethod"), &conmunicate_method);
	conf.Read(("_StoreMethod"), &store_method);
	conf.Read(("_TriggerSource"), &trigger_source);
	conf.Read(("_DataSource"), &data_source);
	conf.Read(("_TriggerType"), &trigger_type);
	conf.Read(("_DiskThreshold"), &disk_threshold);
	conf.Read(("_RecordTime"), &record_time);
	conf.Read(("_RecordFrameNumber"), &record_frame_number);
	conf.Read(("_RecordFrameInterval"), &record_frame_interval);
	conf.Read(("_ExportLog"), &export_log);
	conf.Read(("_ExportHead"), &export_head);
	conf.Read(("_ExportAviLevel"), &export_avi_level);
	conf.Read(("_Password"), &password);
	conf.Read(("_SerialPort"), &serial_port);
	conf.Read(("_LocalTaskScanDepth"), &local_task_scan_depth);
	conf.Read(("_Language"), &language);
	conf.Read(("_SmallPanelSize"), &right_column_size);
	conf.Read(("_DebugDisplayQ"), &debug_display_q);
	conf.Read(("_TimeDecoderFormat"), &time_decoder_format);
	conf.Read(("_ExportBmpType"), &export_bmp_type,0);
	conf.Read(("_DiskMountType"), &disk_mount_type);
	conf.Read(("_NetworkPacketSize"), &network_packet_size);
	conf.Read(("_EnableCameralinkSimulation"), &enable_cameralink_simulation);
	conf.Read(("_EnableFrameLostSignal"), &enable_display_framelost_signal);
	conf.Read(("_EnableWSVersion"), &enable_ws_version);
	conf.Read(("_EnableTimeReplay"), &enable_time_replay);
	conf.Read(("_ExportNameUsingTime"), &export_name_using_time);
	conf.Read(("_LastExportPath"), &last_export_path);
    conf.Read(("_VERSION_CGS"), &m_nVersion);
    conf.Read(("_OverlapText_CGS"), &m_strOverlapText, ("GD220-Ⅱ"));
	conf.Read(("_DebugSerialInfo"), &debug_serial_info);
	// 十字丝
	conf.Read(("_CroseWireColor"), &m_nColor,3);
	conf.Read(("_CroseWireSize"), &m_szSize, "40*40");
	conf.Read(("_CroseWireIsShow"), &m_nShow, 1);
	conf.Read(("_CroseWireLineWidth"), &m_nLineWidth, 1);
	// 导出线程数量 [12/14/2017 xystar]
	conf.Read(("_ExportThreadNum"), &m_nAsyncThreadCnt, 0);

	m_tCrossSize = QSize(40, 40);
	QStringList sizeList = m_szSize.split("*");
	if (sizeList.size() == 2)
	{
		m_tCrossSize = QSize(sizeList[0].toInt(), sizeList[1].toInt());
	}

	QString array_str;
	conf.Read(("_EthName"), &array_str);
	eth_name = array_str.split(INI_SEP, QString::SkipEmptyParts);
	array_str.clear();

	conf.Read(("_DeviceSortOrder"), &array_str);
	device_sort_order = array_str.split(INI_SEP, QString::SkipEmptyParts);
	array_str.clear();

	conf.Read(("_BaseSupportTapBits"), &array_str);
	base_support_tapbits = array_str.split(INI_SEP, QString::SkipEmptyParts);

	array_str.clear();
	conf.Read(("_MediumSupportTapBits"), &array_str);
	medium_support_tapbits = array_str.split(INI_SEP, QString::SkipEmptyParts);
	array_str.clear();

	conf.Read(("_FullSupportTapBits"), &array_str);
	full_support_tapbits = array_str.split(INI_SEP, QString::SkipEmptyParts);
	array_str.clear();

	conf.Read(("_NullSupportTapBits"), &array_str);
	null_support_tapbits = array_str.split(INI_SEP, QString::SkipEmptyParts);
	
	array_str.clear();
	conf.Read(("_ExportTypes"), &array_str);
	export_types = array_str.split(INI_SEP, QString::SkipEmptyParts);

	array_str.clear();
	conf.Read(("_SupportColumn"), &array_str);
	support_column = array_str.split(INI_SEP, QString::SkipEmptyParts);

	array_str.clear();
	conf.Read(("_SupportLine"), &array_str);
	support_line = array_str.split(INI_SEP, QString::SkipEmptyParts);

	array_str.clear();
	conf.Read(("_SupportShrinkLevel"), &array_str);
	support_shrink_level = array_str.split(INI_SEP, QString::SkipEmptyParts);
	
#if DEBUG_CONFIG_FILE
	display_config();
#endif
	load1();
	return true;
}

enum
{
	LIB_CONFIG_DEVICE_CAMERA_UP_DOWN = 0,
	LIB_CONFIG_DEVICE_CAMERA_LEFT_RIGHT,
	LIB_CONFIG_DEVICE_CAMERA_DECODER,
	LIB_CONFIG_DEVICE_INFO_CHANNEL,
	LIB_CONFIG_XGS_EXTRAPARAM,
	LIB_CONFIG_MAX
};

struct config_prefix
{
	int id;
	QString prefix;
}

app_config_prefix[LIB_CONFIG_MAX] =
{
	{ LIB_CONFIG_DEVICE_CAMERA_UP_DOWN, "DeviceCameraUpDown" },
	{ LIB_CONFIG_DEVICE_CAMERA_LEFT_RIGHT, "DeviceCameraCheckLeftRight" },
	{ LIB_CONFIG_DEVICE_CAMERA_DECODER, "DeviceCameraDecoder" },
	{ LIB_CONFIG_DEVICE_INFO_CHANNEL, "DeviceCameraInfoChannel" },
	{ LIB_CONFIG_XGS_EXTRAPARAM, "DeviceExtraExportParams" }
};

static int GetConfigType(wstring str)
{
	for (int i = 0; i < LIB_CONFIG_MAX; i++) {
		if (wstring::npos != str.find(app_config_prefix[i].prefix.toStdWString())) 
		{
			return app_config_prefix[i].id;
		}
	}
	return LIB_CONFIG_MAX;
}

static bool _ParseDeviceCameraBoolConfig(wstring &str, int index, QString &device_serial, int &camera_serial, bool &config)
{
	QString prefix = app_config_prefix[index].prefix;
	unsigned int prefix_len = prefix.length();
	if (str.length() < prefix_len)
		false;

	wstring str_prefix = str.substr(0, prefix_len);
	if (str_prefix != prefix.toStdWString())
		false;

	int pos = str.find_first_of('=');
	if (pos == wstring::npos)
		false;

	int pos_first_ = str.find_first_of('_');
	int pos_second_ = str.find_last_of('_');
	if (pos_first_ == wstring::npos || pos_second_ == wstring::npos)
		return false;
	wstring serial = str.substr(pos_first_ + 1, pos_second_ - pos_first_ - 1);
	wstring camera = str.substr(pos_second_ + 1, pos - pos_second_ - 1);
	wstring status = str.substr(pos + 1);

	device_serial = QString::fromStdWString(serial);
	camera_serial = stoi(camera);
	config = (bool)stoi(status);

	return true;
}

static bool _ParseDeviceCameraIntConfig(wstring &str, int index, QString &device_serial, int &camera_serial, int &config)
{
	QString prefix = app_config_prefix[index].prefix;
	unsigned int prefix_len = prefix.length();
	if (str.length() < prefix_len)
		false;

	wstring str_prefix = str.substr(0, prefix_len);
	if (str_prefix != prefix.toStdWString())
		false;

	int pos = str.find_first_of('=');
	if (pos == wstring::npos)
		false;

	int pos_first_ = str.find_first_of('_');
	int pos_second_ = str.find_last_of('_');
	if (pos_first_ == wstring::npos || pos_second_ == wstring::npos)
		return false;

	wstring serial = str.substr(pos_first_ + 1, pos_second_ - pos_first_ - 1);
	wstring camera = str.substr(pos_second_ + 1, pos - pos_second_ - 1);
	wstring status = str.substr(pos + 1);

	device_serial = QString::fromStdWString(serial);
	camera_serial = stoi(camera);
	config = stoi(status);

	return true;
}

static bool _ParseDeviceCameraName(wstring &str, QString &device_serial, int &camera_serial, wstring &camera_name)
{
	QString prefix = app_config_prefix[LIB_CONFIG_DEVICE_CAMERA_DECODER].prefix;
	unsigned int prefix_len = prefix.length();
	if (str.length() < prefix_len)
		false;

	wstring str_prefix = str.substr(0, prefix_len);
	if (str_prefix != prefix.toStdWString())
		false;

	int pos = str.find_first_of('=');
	if (pos == wstring::npos)
		false;

	int pos_first_ = str.find_first_of('_');
	int pos_second_ = str.find_last_of('_');
	if (pos_first_ == wstring::npos || pos_second_ == wstring::npos)
		return false;
	wstring serial = str.substr(pos_first_ + 1, pos_second_ - pos_first_ - 1);
	wstring camera = str.substr(pos_second_ + 1, pos - pos_second_ - 1);
	wstring name = str.substr(pos + 1);

	device_serial = QString::fromStdWString(serial);
	camera_serial = stoi(camera);
	camera_name = name;

	return true;
}


// 保存类似  各相机通道的  上下——左右——channel——解码
bool EagleConfigFile::load1()
{

#if 1

// 	wifstream stream(CONFIG_FILE_NAME.toStdWString().c_str(), std::ios::binary);
// 	stream.imbue(std::locale(std::locale("chs"), "", LC_CTYPE));
	std::wstring wLine;
	QFile f(CONFIG_FILE_NAME);
	f.open(QIODevice::ReadOnly | QIODevice::Text);
	QTextStream stream(&f);
	
	while (f.isOpen() && !stream.atEnd()) 
	{
		wLine = stream.readLine().trimmed().toStdWString();
		int config_type = GetConfigType(wLine);
		
		switch (config_type) 
		{
		case LIB_CONFIG_DEVICE_CAMERA_UP_DOWN:
		{
			QString device_serial;
			int camera_serial = 0;
			bool check_status = false;
			if (_ParseDeviceCameraBoolConfig(wLine, LIB_CONFIG_DEVICE_CAMERA_UP_DOWN, device_serial, camera_serial, check_status)) 
			{
				try {
					device_camera_up_down_map.at(device_serial);
					try {
						device_camera_up_down_map.at(device_serial).at(camera_serial);
					}
					catch (std::out_of_range&) {
						device_camera_up_down_map.at(device_serial)[camera_serial] = check_status;
					}
				}
				catch (std::out_of_range&) {
					std::map<int, bool> tmp_map;
					tmp_map[camera_serial] = check_status;
					device_camera_up_down_map[device_serial] = tmp_map;
				}
			}
			break;
		}
		case LIB_CONFIG_DEVICE_CAMERA_LEFT_RIGHT:{
			QString device_serial;
			int camera_serial = 0;
			bool check_status = false;
			if (_ParseDeviceCameraBoolConfig(wLine, LIB_CONFIG_DEVICE_CAMERA_LEFT_RIGHT, device_serial, camera_serial, check_status)) 
			{
				try {
					device_camera_left_right_map.at(device_serial);
					try {
						device_camera_left_right_map.at(device_serial).at(camera_serial);
					}
					catch (std::out_of_range&) {
						device_camera_left_right_map.at(device_serial)[camera_serial] = check_status;
					}
				}
				catch (std::out_of_range&) {
					std::map<int, bool> tmp_map;
					tmp_map[camera_serial] = check_status;
					device_camera_left_right_map[device_serial] = tmp_map;
				}
			}
			break;
		}
		case LIB_CONFIG_DEVICE_INFO_CHANNEL:
		{
			QString device_serial;
			int camera_serial = 0;
			int check_status = 0;
			if (_ParseDeviceCameraIntConfig(wLine, LIB_CONFIG_DEVICE_INFO_CHANNEL, device_serial, camera_serial, check_status))
			{
				try {
					device_camera_info_channel_map.at(device_serial);
					try {
						device_camera_info_channel_map.at(device_serial).at(camera_serial);
					}
					catch (std::out_of_range&) {
						device_camera_info_channel_map.at(device_serial)[camera_serial] = check_status;
					}
				}
				catch (std::out_of_range&) {
					std::map<int, int> tmp_map;
					tmp_map[camera_serial] = check_status;
					device_camera_info_channel_map[device_serial] = tmp_map;
				}
			}
			break;
		}
		case LIB_CONFIG_DEVICE_CAMERA_DECODER:{
			QString device_serial;
			int camera_serial = 0;
			wstring camera_name;
			if (_ParseDeviceCameraName(wLine, device_serial, camera_serial, camera_name)) 
			{
				try {
					device_camera_decoder_name_map.at(device_serial);
					try 
					{
						device_camera_decoder_name_map.at(device_serial).at(camera_serial);
					}
					catch (std::out_of_range&) 
					{
						device_camera_decoder_name_map.at(device_serial)[camera_serial] = QString::fromStdWString(camera_name);
					}
				}
				catch (std::out_of_range&) 
				{
					std::map<int, QString> tmp_map;
					tmp_map[camera_serial] = QString::fromStdWString(camera_name);
					device_camera_decoder_name_map[device_serial] = tmp_map;
				}
			}
			break;
		}
		case LIB_CONFIG_XGS_EXTRAPARAM:
		{
			QString device_serial;
			int camera_serial = 0;
			wstring strExtraParams;

			if (_ParseDeviceCameraName(wLine, device_serial, camera_serial, strExtraParams)) {
				try {
					device_camera_xgs_extraParams.at(device_serial);
					try {
						device_camera_xgs_extraParams.at(device_serial).at(camera_serial);
					}
					catch (std::out_of_range&) {
						device_camera_xgs_extraParams.at(device_serial)[camera_serial] = QString::fromStdWString(strExtraParams);
					}
				}
				catch (std::out_of_range&) {
					std::map<int, QString> tmp_map;
					tmp_map[camera_serial] = QString::fromStdWString(strExtraParams);
					device_camera_xgs_extraParams[device_serial] = tmp_map;
				}
			}
			break;
		}
		default:
			break;
		}
	}

#endif // 0
	
	return true;
}

void EagleConfigFile::save(QString strSection,QString strValue)
{
	MySetting conf(CONFIG_FILE_NAME);
	if (conf.IsOk() == false)
	{
		return;
	}

	conf.Write(strSection, strValue);
}


void EagleConfigFile::store()
{
	MySetting conf(CONFIG_FILE_NAME);
	if (conf.IsOk() == false)
	{
        return;
    }
	conf.remove("General");
	conf.Write(("WHAT"), ("This is the configure file for Eagle"));
	conf.Write(("_TaskName"), task_name);
	conf.Write(("_ConmunicateMethod"), conmunicate_method);
	conf.Write(("_StoreMethod"), store_method);
	conf.Write(("_TriggerSource"), trigger_source);
	conf.Write(("_DataSource"), data_source);
	conf.Write(("_TriggerType"), trigger_type);
	conf.Write(("_DiskThreshold"), disk_threshold);
	conf.Write(("_RecordTime"), record_time);
	conf.Write(("_RecordFrameNumber"), record_frame_number);
	conf.Write(("_RecordFrameInterval"), record_frame_interval);
	conf.Write(("_ExportLog"), export_log);
	conf.Write(("_ExportHead"), export_head);
	conf.Write(("_ExportAviLevel"), export_avi_level);
	conf.Write(("_Password"), password);
	conf.Write(("_SerialPort"), serial_port);
	conf.Write(("_EthName"), eth_name);
	conf.Write(("_LocalTaskScanDepth"), local_task_scan_depth);
	conf.Write(("_SmallPanelSize"), right_column_size);
	conf.Write(("_Language"), language);
	conf.Write(("_DebugDisplayQ"), debug_display_q);
	conf.Write(("_TimeDecoderFormat"), time_decoder_format);
	conf.Write(("_ExportBmpType"), export_bmp_type);
	conf.Write(("_DeviceSortOrder"), device_sort_order);
	conf.Write(("_DiskMountType"), disk_mount_type); 
	conf.Write(("_NetworkPacketSize"), network_packet_size);
	conf.Write(("_EnableCameralinkSimulation"), enable_cameralink_simulation);
	conf.Write(("_EnableFrameLostSignal"), enable_display_framelost_signal);
	conf.Write(("_EnableWSVersion"), enable_ws_version);
	conf.Write(("_EnableTimeReplay"), enable_time_replay);
	conf.Write(("_ExportNameUsingTime"), export_name_using_time);
	conf.Write(("_LastExportPath"), last_export_path);
    conf.Write(("_VERSION_CGS"), m_nVersion);
    conf.Write(("_OverlapText_CGS"), m_strOverlapText);
	conf.Write(("_DebugSerialInfo"), debug_serial_info);
	conf.Write(("_BaseSupportTapBits"), base_support_tapbits);
	conf.Write(("_MediumSupportTapBits"), medium_support_tapbits);
	conf.Write(("_FullSupportTapBits"), full_support_tapbits);
	conf.Write(("_NullSupportTapBits"), null_support_tapbits);
	conf.Write(("_ExportTypes"), export_types);
	conf.Write(("_SupportColumn"), support_column);
	conf.Write(("_SupportLine"), support_line);
	conf.Write(("_SupportShrinkLevel"), support_shrink_level);
	conf.Write(("_CroseWireColor"), m_nColor);
	conf.Write(("_CroseWireSize"), m_szSize);
	conf.Write(("_CroseWireIsShow"), m_nShow); 
	conf.Write(("_CroseWireLineWidth"), m_nLineWidth);
	conf.Write(("_ExportThreadNum"), m_nAsyncThreadCnt);
	// 以下先保障编译通过  字符 编码没有考虑
	for (auto dev_it = device_camera_left_right_map.begin(); dev_it != device_camera_left_right_map.end(); dev_it++)
	{
		for (auto cam_it = dev_it->second.begin(); cam_it != dev_it->second.end(); cam_it++)
		{
			QString key = QString("%1_%2_%3").arg(app_config_prefix[LIB_CONFIG_DEVICE_CAMERA_LEFT_RIGHT].prefix,
				dev_it->first).arg(cam_it->first);
			QString new_value = QString::number(cam_it->second);
			
			conf.Write(key, new_value);
		}
	}

	for (auto dev_it = device_camera_up_down_map.begin(); dev_it != device_camera_up_down_map.end(); dev_it++)
	{

		for (auto cam_it = dev_it->second.begin(); cam_it != dev_it->second.end(); cam_it++)
		{
			QString key = QString("%1_%2_%3").arg(app_config_prefix[LIB_CONFIG_DEVICE_CAMERA_UP_DOWN].prefix,
				dev_it->first).arg(cam_it->first);
			QString new_value = QString::number(cam_it->second);
			
			conf.Write(key, new_value);
		}
	}

	for (auto dev_it = device_camera_info_channel_map.begin(); dev_it != device_camera_info_channel_map.end(); dev_it++)
	{

		for (auto cam_it = dev_it->second.begin(); cam_it != dev_it->second.end(); cam_it++)
		{
			QString key = QString("%1_%2_%3").arg(app_config_prefix[LIB_CONFIG_DEVICE_INFO_CHANNEL].prefix,
				dev_it->first).arg(cam_it->first);
			QString new_value = QString::number(cam_it->second);
			
			conf.Write(key, new_value);
		}
	}

	for (auto dev_it = device_camera_decoder_name_map.begin(); dev_it != device_camera_decoder_name_map.end(); dev_it++)
	{

		for (auto cam_it = dev_it->second.begin(); cam_it != dev_it->second.end(); cam_it++)
		{
			QString key = QString("%1_%2_%3").arg(app_config_prefix[LIB_CONFIG_DEVICE_CAMERA_DECODER].prefix,
				dev_it->first).arg(cam_it->first);
			
			conf.Write(key, cam_it->second);
		}
	}

	for(auto it = ccd_config.begin(); it != ccd_config.end(); it ++)
	{
		conf.Write(PRIFIX + it->second.serial + ("_Name"), it->second.name);
		conf.Write(PRIFIX + it->second.serial + ("_PixLine"), it->second.pix_line);
		conf.Write(PRIFIX + it->second.serial + ("_PixColumn"), it->second.pix_col);
		conf.Write(PRIFIX + it->second.serial + ("_Taps"), it->second.taps);
		conf.Write(PRIFIX + it->second.serial + ("_ColorDepth"), it->second.color_depth);
	}

#ifdef  XIANGS_VERSION

	for (auto dev_it = device_camera_xgs_extraParams.begin(); dev_it != device_camera_xgs_extraParams.end(); dev_it++)
	{

		for (auto cam_it = dev_it->second.begin(); cam_it != dev_it->second.end(); cam_it++)
		{
			QString key = QString("%1_%2_%3")
				.arg(app_config_prefix[LIB_CONFIG_XGS_EXTRAPARAM].prefix,dev_it->first)
				.arg(cam_it->first);
			conf.Write(key, cam_it->second);
		}
	}

#endif

	conf.sync();
}

bool EagleConfigFile::GetDeviceCameraLeftRightReverse(int device_id, int ccd_serial)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return false;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return false;
	return GetDeviceCameraLeftRightReverse(device_serial, ccd_serial);
}

bool EagleConfigFile::GetDeviceCameraUpDownReverse(int device_id, int ccd_serial)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return false;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return false;
	return GetDeviceCameraUpDownReverse(device_serial, ccd_serial);
}

QString EagleConfigFile::GetDeviceCameraDecoderName(int device_id, int ccd_serial)
{
	char device_serial[32] = { '\0' };
	QString string_ret;
	if (ccd_serial < 1)
		return string_ret;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return string_ret;
	return GetDeviceCameraDecoderName(device_serial, ccd_serial);
}

int EagleConfigFile::GetDeviceCameraInfoChannel(int device_id, int ccd_serial)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return 1;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return 1;
	return GetDeviceCameraInfoChannel(device_serial, ccd_serial);
}

bool EagleConfigFile::GetDeviceCameraLeftRightReverse(QString device_serial, int ccd_serial)
{
	try {
		return device_camera_left_right_map.at(device_serial).at(ccd_serial);
	}
	catch (std::out_of_range&) {
		return false;
	}
}

bool EagleConfigFile::GetDeviceCameraUpDownReverse(QString device_serial, int ccd_serial)
{
	try {
		return device_camera_up_down_map.at(device_serial).at(ccd_serial);
	}
	catch (std::out_of_range&) {
		return false;
	}
}

QString EagleConfigFile::GetDeviceCameraDecoderName(QString device_serial, int ccd_serial)
{
	try {
		return device_camera_decoder_name_map.at(device_serial).at(ccd_serial);
	}
	catch (std::out_of_range&) {
		return "";
	}
}

int EagleConfigFile::GetDeviceCameraInfoChannel(QString device_serial, int ccd_serial)
{
	try {
		return device_camera_info_channel_map.at(device_serial).at(ccd_serial);
	}
	catch (std::out_of_range&) {
		return 1;
	}
}

void EagleConfigFile::SetDeviceCameraLeftRightReverse(int device_id, int ccd_serial, bool valid)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return;
	try {
		device_camera_left_right_map.at(device_serial);
		try {
			device_camera_left_right_map.at(device_serial).at(ccd_serial) = valid;
		}
		catch (std::out_of_range&) {
			device_camera_left_right_map.at(device_serial)[ccd_serial] = valid;
		}
	}
	catch (std::out_of_range&) {
		std::map<int, bool> tmp_map;
		tmp_map[ccd_serial] = valid;
		device_camera_left_right_map[device_serial] = tmp_map;
	}
	store();
}

void EagleConfigFile::SetDeviceCameraUpDownReverse(int device_id, int ccd_serial, bool valid)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return;
	try {
		device_camera_up_down_map.at(device_serial);
		try {
			device_camera_up_down_map.at(device_serial).at(ccd_serial) = valid;
		}
		catch (std::out_of_range&) {
			device_camera_up_down_map.at(device_serial)[ccd_serial] = valid;
		}
	}
	catch (std::out_of_range&) {
		std::map<int, bool> tmp_map;
		tmp_map[ccd_serial] = valid;
		device_camera_up_down_map[device_serial] = tmp_map;
	}
	store();
}

void EagleConfigFile::SetDeviceCameraInfoChannel(int device_id, int ccd_serial, int new_channel)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return;
	try {
		device_camera_info_channel_map.at(device_serial);
		try {
			device_camera_info_channel_map.at(device_serial).at(ccd_serial) = new_channel;
		}
		catch (std::out_of_range&) {
			device_camera_info_channel_map.at(device_serial)[ccd_serial] = new_channel;
		}
	}
	catch (std::out_of_range&) {
		std::map<int, int> tmp_map;
		tmp_map[ccd_serial] = new_channel;
		device_camera_info_channel_map[device_serial] = tmp_map;
	}
	store();
}

void EagleConfigFile::SetDeviceCameraDecoderName(int device_id, int ccd_serial, QString decoder_name)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return;
	try {
		device_camera_decoder_name_map.at(device_serial);
		try {
			device_camera_decoder_name_map.at(device_serial).at(ccd_serial) = decoder_name;
		}
		catch (std::out_of_range&) {
			device_camera_decoder_name_map.at(device_serial)[ccd_serial] = decoder_name;
		}
	}
	catch (std::out_of_range&) {
		std::map<int, QString> tmp_map;
		tmp_map[ccd_serial] = decoder_name;
		device_camera_decoder_name_map[device_serial] = tmp_map;
	}
	store();
}

void EagleConfigFile::SetDeviceCameraExtraParam(int device_id, int ccd_serial, QString extraParams)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return;
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return;
	try {
		device_camera_xgs_extraParams.at(device_serial);
		try {
			device_camera_xgs_extraParams.at(device_serial).at(ccd_serial) = extraParams;
		}
		catch (std::out_of_range&) {
			device_camera_xgs_extraParams.at(device_serial)[ccd_serial] = extraParams;
		}
	}
	catch (std::out_of_range&) {
		std::map<int, QString> tmp_map;
		tmp_map[ccd_serial] = extraParams;
		device_camera_xgs_extraParams[device_serial] = tmp_map;
	}
	store();
}

QString EagleConfigFile::GetDeviceCameraExtraParam(int device_id, int ccd_serial)
{
	char device_serial[32] = { '\0' };
	if (ccd_serial < 1)
		return "";
	int ret = EagleControl_GetDeviceSerial(device_id, device_serial);
	if (0 != ret)
		return "";

	try {
		return device_camera_xgs_extraParams.at(device_serial).at(ccd_serial);
	}
	catch (std::out_of_range&) {
		return "";
	}
}

void EagleConfigFile::saveCroseWire()
{
	MySetting conf(CONFIG_FILE_NAME);
	if (conf.IsOk() == false)
	{
		return;
	}
	conf.Write(("_CroseWireColor"), m_nColor);
	conf.Write(("_CroseWireSize"), m_szSize);
	conf.Write(("_CroseWireIsShow"), m_nShow);
	conf.Write(("_CroseWireLineWidth"), m_nLineWidth);
}

